/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccGetResInfoGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCreateNewGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorDataGetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorDataSetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AccessorInitializeGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AcsCopyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.AccessorDatePropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.AccessorDateTimePropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.AccessorExplicitPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.DynamicPropAcsPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.InnerDataGeneratpr;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NestedAccessorFieldGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NestedAccessorPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.ReadonlyPropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.BaseDataGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.DataTypeImpContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.BaseTypePropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.DataTypePropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.DynamicPropSetPropertyGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.util.ArrayList;
import java.util.List;

import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import org.eclipse.jdt.core.dom.Modifier;

public abstract class BaseAcsGenerator extends BaseDataGenerator
{

	public BaseAcsGenerator(EntityGeneratorContext parContext, IGspCommonDataType node, String nodeCode, MediateType targetInterfaceType)
	{
		super(parContext, node, targetInterfaceType);
		if (addToMediateTypes())
		{
			parContext.getProjContext().getAccessorMediateTypes().put(node.getCode(), new MediateType(getNameSpace(), getName()));
		}
	}

	public BaseAcsGenerator(EntityGeneratorContext parContext, String nodeCode, MediateType targetInterfaceType)
	{
		super(parContext, nodeCode, targetInterfaceType);
		if (addToMediateTypes()) {
			parContext.getProjContext().getAccessorMediateTypes().put(nodeCode, new MediateType(getNameSpace(), getName()));
		}
	}

	protected boolean addToMediateTypes(){return true;}

	protected String getNameSpace()
	{
		return "";
	}

	@Override
	protected String getName()
	{
		//return String.format("%1$s_%2$sAccessor", GeneratorUtil.getName(ParentContext.getProjContext().getCefConfig().getID()),GeneratorUtil.remove(TargetInterfaceType.getSimpleName()));
		return String.format("%1$sAcc", GeneratorUtil.remove(TargetInterfaceType.getClassName()));
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list=new ArrayList<Modifier.ModifierKeyword>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected DataTypePropGenerator getDataTypePropGenerator(MediatePropertyInfo propertyInfo,
			String fieldName)
	{
		return new NestedAccessorPropertyGenerator(propertyInfo);
	}

//	@Override
//	protected BaseTypePropGenerator getBaseTypePropGenerator(MediatePropertyInfo propertyInfo)
//	{
//		if (propertyInfo.getElement().getMDataType()== GspElementDataType.Date)
//			return AccessorDatePropertyGenerator.createAccessorDateProperty(propertyInfo);
//		if (propertyInfo.getElement().getMDataType()==GspElementDataType.DateTime)
//			return AccessorDateTimePropertyGenerator.createAccessorDateProperty(propertyInfo);
//		return AccessorExplicitPropertyGenerator.createImplicit(propertyInfo);
//	}

	@Override
	protected List<BaseTypePropGenerator> getBaseTypePropGenerators(MediatePropertyInfo propertyInfo)
	{
		List<BaseTypePropGenerator> baseTypePropGenerators = new ArrayList<>();
		if (propertyInfo.getElement().getMDataType()== GspElementDataType.Date)
		{
			baseTypePropGenerators.add(AccessorDatePropertyGenerator.createAccessorDateProperty(propertyInfo,ParentContext,Node.getCode()));
			if(Character.isLowerCase(propertyInfo.getPropertyName().charAt(0)) && !propertyInfo.getExtendInfos().containsKey("AssoUdtAtrribute"))
			{
				baseTypePropGenerators.add(AccessorDatePropertyGenerator.createAccessorDateProperty(propertyInfo,ParentContext,NodeCode, true));
			}
			return baseTypePropGenerators;
		}

		if (propertyInfo.getElement().getMDataType()==GspElementDataType.DateTime)
		{
			baseTypePropGenerators.add(AccessorDateTimePropertyGenerator.createAccessorDateProperty(propertyInfo,ParentContext,NodeCode));
			if(Character.isLowerCase(propertyInfo.getPropertyName().charAt(0)) && !propertyInfo.getExtendInfos().containsKey("AssoUdtAtrribute"))
			{
				baseTypePropGenerators.add(AccessorDateTimePropertyGenerator.createAccessorDateProperty(propertyInfo,ParentContext,NodeCode, true));
			}
			return baseTypePropGenerators;
		}
		baseTypePropGenerators.add(AccessorExplicitPropertyGenerator.createImplicit(ParentContext,NodeCode, propertyInfo));
		if(Character.isLowerCase(propertyInfo.getPropertyName().charAt(0)) && !propertyInfo.getExtendInfos().containsKey("AssoUdtAtrribute"))
		{
			baseTypePropGenerators.add(AccessorExplicitPropertyGenerator.createImplicit(ParentContext,NodeCode, propertyInfo, true));
		}
		return baseTypePropGenerators;
	}

	@Override
	protected void addExtendPropertyGenerator(java.util.ArrayList<ClassPropertyGenerator> proGenerators)
	{

		//private new ISalesOrder InnerData => (ISalesOrder)base.InnerData;
		//todo:gensim
//		InnerDataGeneratpr innerDataGen = new InnerDataGeneratpr(ExtendTypes == null || ExtendTypes.isEmpty() ? TargetInterfaceType : ExtendTypes.get(ExtendTypes.size()-1));
		InnerDataGeneratpr innerDataGen = new InnerDataGeneratpr(TargetInterfaceType);
		proGenerators.add(innerDataGen);
	}

//	@Override
//	protected java.util.ArrayList<FieldGenerator> createFieldGenerator()
//	{

//		java.util.ArrayList<FieldGenerator> list = ((super.createFieldGenerator()) != null) ? super.createFieldGenerator() : new java.util.ArrayList<FieldGenerator>();
//
//		for(MediatePropertyInfo field: PropertyInfos)
//		{
//			if(field .getElement() == null || !field.getElement().getIsUdt())
//				continue;
//			list.add(new NestedAccessorFieldGenerator(NodeCode,field .getElement(),ParentContext));
//		}
//
//		return list;
//	}

	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators()
	{

		ArrayList<ClassMethodGenerator> methods = ((super.createMethodGenerators()) != null) ? super.createMethodGenerators() : new java.util.ArrayList<ClassMethodGenerator>();
		ClassMethodGenerator getValueGen = getAccessorDataGetValueGenerator();
		ClassMethodGenerator setValueGen = getAccessorDataSetValueGenerator();
		methods.add(getValueGen);
		ReadonlyPropGenerator readOnlyGen = new ReadonlyPropGenerator(false);
		methods.add(readOnlyGen);
		methods.add(setValueGen);
		methods.add(getInitilizeGenerator());
		methods.add(getRestGenerator());
		methods.add(new AccessorCreateNewGenerator(getName()));
		addAcsExtendMethod(methods);
		ClassMethodGenerator resGen = get_GetResInfo_Generator();
		if(resGen != null) {
			methods.add(resGen);
		}
		return methods;
	}

	protected  ClassMethodGenerator getAccessorDataGetValueGenerator()
	{
		return new AccessorDataGetValueGenerator(ParentContext,NodeCode);
	}

	protected  ClassMethodGenerator getAccessorDataSetValueGenerator()
	{
		return new AccessorDataSetValueGenerator(ParentContext,NodeCode);
	}

	public abstract AccessorInitializeGen getInitilizeGenerator();


	public AcsCopyGenerator getRestGenerator(){return new AcsCopyGenerator(getName(), NodeCode, PropertyInfos, ParentContext);}

	protected void addAcsExtendMethod(java.util.ArrayList<ClassMethodGenerator> methodGens)
	{

	}

	@Override
	protected ArrayList<TypeInfo> addParentInterfaces() {
		ArrayList<TypeInfo> list= super.addParentInterfaces();
		if(list==null)
			list=new ArrayList<TypeInfo>();
		TypeInfo info =new TypeInfo(TargetInterfaceType.getFullName());
		info.setUseFullName(true);
		list.add(info);
//		for(Class type:ExtendTypes)
//		{
//			TypeInfo info1 =new TypeInfo(type.getTypeName());
//			info.setUseFullName(true);
//			list.add(info1);
//		}
		return list;
	}

	@Override
	protected void addEnrichedAssoUdtGenerator(ArrayList<BaseGenerator> list, MediatePropertyInfo propInfo) {
		list.add(new EnrichedAssoUdtAcsGenerator(ParentContext, propInfo, getName()));
	}

	@Override
	protected DynamicPropSetPropertyGenerator getDynamicPropSetPropertyGenerator(
			MediatePropertyInfo propertyInfo) {
		return
				new DynamicPropAcsPropertyGenerator(propertyInfo);
	}

	protected ClassMethodGenerator get_GetResInfo_Generator(){
		if(ParentContext.getProjContext().getEntityMediateTypes().get(NodeCode) != null) {
			return new AccGetResInfoGenerator(ParentContext, NodeCode);
		}
		return null;
	}
}
